package com.xzx.email;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送第二份邮件.使用另外一种方式
 *
 * @author xinzhixuan
 * @version V1.0
 * @date 2017/9/20 21:48
 */
public class SendSecondEmail {

    public static void main(String[] args) throws MessagingException {

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.host", "smtp.sina.com");
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xinzhixuan29", "92803991nihao");
            }
        });
        session.setDebug(true);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("xinzhixuan29@sina.com"));
        message.setSubject("第二份邮件");
        message.setRecipients(Message.RecipientType.TO,
                              InternetAddress.parse("xinzhixuan29@163.com,xinzhixuan29@sina.com"));
        message.setContent("<span style='color:red;'>第二份邮件</span>", "text/html;charset=utf-8");

        Transport.send(message);
    }
}
